<?php

class RestrictWithdrawal extends Extension {
	protected $available_days = array(1, 2, 15, 16);
	private static $allowed_actions = array(
        'restrict'
    );
	
	function onAfterInit(){
		if(!$this->owner->redirectedTo() && $this->owner->request->param('Action') != 'restrict' && !in_array(date('j'), $this->available_days)){
			return $this->owner->redirect($this->owner->Link('restrict'));
		}
	}
	
	function restrict(){
		return array('Form' => $this->RestrictForm());
	}
	
	function RestrictForm(){
		$fields = FieldList::create(
			LiteralField::create('RestrictAlert', sprintf('<div class="alert alert-danger" role="alert"><span class="glyphicon glyphicon-warning-sign"></span> %s</div>', _t('RestrictWithdrawal.RESTRICT_ALERT', 'Sorry, you are only allowed to submit withdrawal on the {available_days} of the month.', '', array('available_days' => implode(', ', $this->available_days)))))
		);
		$actions = FieldList::create();

        return Form::create($this->owner, 'RestrictForm', $fields, $actions);
	}
}
